import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '@/views/Login'
import Layout from '@/views/Layout'
import Home from '@/views/Layout/Home'
import User from '@/views/Layout/User'
import Search from '@/views/Search'
import SearchResult from '@/views/Search/SearchResult.vue'
import ArticleDetail from '@/views/ArticleDetail'
import UserEdit from '../views/Layout/User/UserEdit.vue'
import Chat from '../views/Chat'
import store from '../store'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: 'login'
  },
  {
    path: '/login',
    // component: Login
    component: () => import('@/views/Login'),
    beforeEnter (to, from, next) {
      if (store.state.token.length > 0) {
        // vuex里有token(代表登录过, 但是一定要注意过期和主动退出要先清除vuex和本地的token, 让其跳转登录页)
        return next(false)
      }
      next()
    }
  },
  {
    path: '/layout',
    component: Layout,
    redirect: '/layout/home',
    children: [
      {
        path: 'home',
        component: Home
      },
      {
        path: 'user',
        component: User
      }
    ]
  },
  {
    path: '/search',
    component: Search
  }, {
    path: '/search/:kw',
    component: SearchResult
  },
  {
    path: '/article_detail/:kws',
    component: ArticleDetail
  }, {
    path: '/user/edit',
    component: UserEdit
  },
  {
    path: '/chat',
    component: Chat
  }
]

const router = new VueRouter({
  routes
})

export default router
